package com.xyzwps.paimon.furina.infra

import org.redisson.RedissonBucket
import org.redisson.api.RFuture
import org.redisson.client.codec.Codec
import org.redisson.client.protocol.RedisCommands
import org.redisson.command.CommandAsyncExecutor

class EnchantedRBucket<T>(commandExecutor: CommandAsyncExecutor, name: String, codec: Codec) :
    RedissonBucket<T>(codec, commandExecutor, name) {

    fun setIfExistsAndKeepTTLAsync(value: T): RFuture<Void> {
        if (value == null) {
            return commandExecutor.writeAsync(rawName, RedisCommands.DEL_VOID, rawName);
        }
        return commandExecutor.writeAsync(
            rawName, codec, RedisCommands.SET,
            rawName, encode(value), "XX", "KEEPTTL"
        )
    }

    fun setIfExistsAndKeepTTL(value: T): Unit {
        get(setIfExistsAndKeepTTLAsync(value))
    }
}